/* @flow */

import { parse } from './parser/index'
import { optimize } from './optimizer'
import { generate } from './codegen/index'
import { createCompilerCreator } from './create-compiler'

// `createCompilerCreator` allows creating compilers that use alternative
// parser/optimizer/codegen, e.g the SSR optimizing compiler.
// Here we just export a default compiler using the default parts.
// createCompilerCreator 接收一个函数作为参数，真正的编译过程在 函数baseCompile 中
// 将templete转为render函数
export const createCompiler = createCompilerCreator(function baseCompile (
  template: string,
  options: CompilerOptions
): CompiledResult {
  // parse编译，生成AST语法树
  const ast = parse(template.trim(), options)
  // 引用对象，下一步对ast进行的优化，会改变ast对象值
  console.log(Object.assign({}, ast), "ast object")
  if (options.optimize !== false) {
    // 对ast进行优化，可以在patch阶段跳过一些节点的对比
    optimize(ast, options)
  }
  // 将ast对象转为执行代码
  const code = generate(ast, options)
  // render最后转化为
  /* 
    with(this){
      return (isShow)?
        _c('ul', 
          {staticClass:"list",
            class:bindCls},
          _l((list),function(item,index) 
            { return _c('li',
              { on: { 
                "click":function($event){
                  return clickItem(index) 
                }
              }
            },
          [_v(" "+_s(item)+": "+_s(index))]
      )}),0) : _e()
    } 
  */
  return {
    ast,
    render: code.render,
    staticRenderFns: code.staticRenderFns
  }
})
